<?php
/**
*
* @author J.Ducastel <fredbird@fredbird.org>
* @version
* @uses curl
* @uses domxml
*/
class ooXmlRpcClient {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	* /
	function ooXmlRpcClient() {
		$this->__construct();
	}/**/

	/**
	*
	* @access
	* /
	function __construct() {

	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* sets url for xml-rpc calls
	* @param string $url
	* @access public
	* @return bool
	*/
	function setUrl($url) {
		$this->url=$url;
		return true;
	}/**/

	/**
	* builds methodCall from method name + params
	* @param string $methodName
	* @param array optional, parameters array
	* @access public
	* @return bool
	*/
	function setCall($methodName,$params=array()) {
		// searching for custom call-building method
		// fall back to standard method
		if (!$call=$this->buildCall($methodName,$params)) {
			return false;
		}
		$this->methodCall=&$call;
		//echo wakaffvar($call);
		return true;
	}/**/

	/**
	* imports pre-built methodCall
	* @param ooXmlRpcMethodCall
	* @access public
	* @return bool
	*/
	function setMethodCall(& $methodCall) {
		if (!is_a($methodCall,'ooXmlRpcMethodCall'))
			return false;
		$this->methodCall=&$methodCall;
		return true;
	}/**/

	/**
	* sets http authentification data
	* @param
	* @access public
	* @return bool
	*/
	function setAuth($login,$pwd) {
		if (!$login or !$pwd) {
			return false;
		}
		$this->auth=$login.':'.$pwd;
		return true;
	}/**/

	/**
	* executes call
	* @access public
	* @return bool
	*/
	function issueCall() {
		// getting http request result
		if (!$this->getHttpResponse()) {
			return false;
		}
		// building methodResponse
		//$this->
		return true;
	}/**/

	/**
	*
	* @param
	* @access public
	* @return
	*/
	function getHttpResponse() {
		if (!$this->httpResponse) {
			if (!is_a($this->methodCall,'ooXmlRpcMethodCall')) {
				return false;
			}
			$xml=$this->methodCall->getXml(); //echo wakaffvar($xml);
			$this->httpResponse=$this->httpXmlRpcRequest($xml,$this->url);
		}
		return $this->httpResponse;
	}/**/

	/**
	* returns response as an object
	* @access public
	* @return ooXmlRpcMethodResponse
	*/
	function getMethodResponse() {
		if (!$this->methodResponse) {
			$xml=$this->getHttpResponse();
			$response=new ooXmlRpcMethodResponse();
			if ($response->setFromXml($xml))
				$this->methodResponse=&$response;
		}
		return $this->methodResponse;
	}/**/

	/**
	*
	* @param
	* @access public
	* @return
	* /
	function () {

	}/**/


/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var string default/current url
	*/
	var $url;
	/**
	* @var ooXmlRpcMethodCall call instance
	*/
	var $methodCall;
	/**
	* @var string http response content
	*/
	var $httpResponse;
	/**
	* @var ooXmlRpcMethodResponse response instance
	*/
	var $methodResponse;
	/**
	* @var string http authentication as login:pwd
	*/
	var $auth;
	/**
	* @var type desc
	*/
	/**
	* @var type desc
	*/
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* Issue server xml-rpc request and return http response content
	* uses curl library
	* @param string $url xml-rpc url
	* @param string $xml complete methodCall xml
	* @access protected
	* @return string
	*/
	function httpXmlRpcRequest($xml,$url=null) {
		if (is_null($url))
			$url=$this->url;
		// init curl session
		$ch =&curl_init($url);
		// setting correct xml-rpc parameters
		// POST call
		curl_setopt($ch,CURLOPT_POST,true);
		// headers
		$headers=array(
			'Content-type : text/xml'
		);
		curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
		// setting raw POST data
		curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
		// http authentication
		if ($this->auth)
			curl_setopt($ch,CURLOPT_USERPWD,$this->auth);

		// executing http call
		// returning content rather than printing it
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		$response=curl_exec($ch);
		$info=curl_getinfo($ch);
		//$deb=wak::getservice('wakdebug');
		// echo $deb->vardump($info);
		// echo wakaffvar($info);
		curl_close($ch);
		return $response;
	}/**/

	/**
	* builds methodCall
	* @param string $name
	* @access protected
	* @return ooXmlRpcMethodCall
	* /
	function buildCall($name,$params=array()) {

	}/**/

	/**
	* returns response to methodcall, as xml
	* @param
	* @access protected
	* @return
	*/
	function & getResponseXmlToCall($methodCall,$url=null) {
		if (is_null($url))
			$url=$this->url;
		// getting xml
		$callXml=$methodCall->getXml(); //echo $callXml;
		// sending call
		$responseXml=$this->httpXmlRpcRequest($callXml,$url);
		// echo $responseXml;
		return $responseXml;
	}/**/

	/**
	* returns response to methodcall, as methodResponse
	* @param
	* @access protected
	* @return
	*/
	function & getResponseToCall(& $methodCall, $url=null) {
		if (is_null($url))
			$url=$this->url;
		$xml=$this->getResponseXmlToCall($methodCall,$url);
		// echo htmlentities($responseXml);
		//echo $responseXml;
		// building response
		$response=new ooXmlRpcMethodResponse();
		$response->setFromXml($xml);
		// echo wakaffvar($response);
		return $response;
	}/**/

	/**
	* builds methodCall instance
	* @param string $methodName
	* @param array $params
	* @access protected
	* @return ooXmlRpcMethodCall
	*/
	function & buildCall($methodName,$params=array()) {
		$call=new ooXmlRpcMethodCall();
		$call->setName($methodName);
		for ($n=0; $n<count($params); $n++) {
			$call->params->setParam($params[$n]);
		}
		// echo wakaffvar($call);
		return $call;
	}/**/
}
?>
